<?php
// include('../func/utils.php');

// 生成数据库盐字段字符
function createSalt(){
    $saltStr = createRandStr(30);
    $pos = mt_rand(0, 9);
    $saltLine = $saltStr . ',' . $pos;
    return $saltLine; 
}

// 生成数据库密码字段
/*
$salt: 使用createSalt()生成的盐字段
$pwd: 用户密码
*/
function encryptionPwd($salt = '', $pwd = '123456'){
    $tmpArray = explode(',', $salt);


    $pwd = md5(substr_replace($pwd, $tmpArray[0], $tmpArray[1], 0));
    return $pwd;
}

// 更新用户密码
/*
{String} $userId: 学号或工号
{String} $pwd: 新用户密码
{String} $role: 用户角色（老师或学生。。。）
*/
function updateUserPwd($userId, $pwd, $role){
    include('../data/dbfunc.php');
    include('../data/dbparam.php');
    
    $conn = conn_mysql();

    // 盐字段
    $salt = createSalt(30);
    // 使用盐加密后的密码
    $encrypPwd = encryptionPwd($salt, $pwd);

    if($role == '0'){

        $sql = "
                update
                    huangcz_stu 
                set 
                    salt = '{$salt}',
                    stu_pwd = '{$encrypPwd}'
                where
                    stu_id = '{$userId}'
        ";
    }else{
        $sql = "
                update
                    huangcz_stu 
                set 
                    salt = '{$salt}',
                    te_pwd = '{$encrypPwd}'
                where
                    te_id = '{$userId}'
        ";
    }
    // 执行sql语句
    $conn->query($sql);
}



